package com.chixing.controller;

import com.chixing.pojo.*;
import com.chixing.service.CartService;
import com.chixing.service.OrderDetailService;
import com.chixing.service.OrderService;
import com.chixing.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class OrderController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private CartService cartService;
    @Autowired
    private ProductService productService;


    @PostMapping("/orders")
    public ModelAndView save(Integer productSum, HttpSession session){
        System.out.println("订单总金额" + productSum);  //  订单总金额

        Myorder myorder = new Myorder();
        Customer loginCustomer = (Customer) session.getAttribute("loginCustomer");
        Integer CustomerId = loginCustomer.getCustumerId();
        String CustomerTel = (String)loginCustomer.getCustumerTel();
        String orderNum = UUID.randomUUID().toString().replace("-","");
        String orderId = orderNum.substring(22);
        System.out.println("uuid截取字符串" + orderId);

        myorder.setOrderId(orderId);
        myorder.setOrderNo(orderNum);
        myorder.setCustumerId(CustomerId);
        myorder.setCustumerTel(CustomerTel);
        myorder.setOrderIssuedDate(new Date());
        myorder.setOrderPayStatus(0);   //  "0"待支付  “1" 已支付
        System.out.println("最新的订单：" + myorder);



        Myorder newOrders = orderService.save(myorder);
        System.out.println("添加到数据库后，订单的信息：" + newOrders);

//        查看订单id
        String idd = newOrders.getOrderId();
        System.out.println("当前订单的idd==============" +idd);


//      获取购物车内数据 --- list<Cart> ---> 数据保存到订单详情
        List<Cart> cartList = cartService.getByCustomerId(CustomerId);
//      添加订单详情表 --- save
        int sum = 0;  //      定义一个商品总数变量 计算商品总数
        int priceSum = 0;  //      定义一个订单总价变量 计算订单总价
        for ( Cart cart :cartList
             ) {
//      商品id   ---  商品数量 ---  订单总价
           Integer productId= cart.getProductId();
           Integer productNumber = cart.getProductNumber();

           sum += productNumber;
//      根据商品id获取商品   名称，单价，图片链接
            Product product = productService.getbyid(productId);
            String proName = product.getProductName();
            Double proPrice = product.getProductPrice();
            String proImage = product.getProductPic();

            priceSum += proPrice;
//      数据保存到订单详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderNo(orderNum);
            orderDetail.setProductName(proName);
            orderDetail.setProductImg(proImage);
            orderDetail.setProductPrice(proPrice);
            orderDetail.setProductNum(productNumber);
            System.out.println(orderDetail);
            boolean result = orderDetailService.save(orderDetail);
            System.out.println("true 为添加成功" + result);
        }



        newOrders.setProductAmount(priceSum);
        System.out.println("订单总金额" + priceSum);
//        String result = orderService.update(orderId) ;
//        System.out.println(result);
//        myorder.setProductAmount();  //订单总金额




//        订单编号,订单总价,商品总数,订单详情list
        List<OrderDetail> orderDetails = orderDetailService.selectByOrderNum(orderNum);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("myorders/orderdetails");
        mav.addObject("orderId",idd);     // 订单id
        mav.addObject("orderNum",orderNum);   // 订单编号
        mav.addObject("priceSum",priceSum);   // 订单总金额
        mav.addObject("orderDetails",orderDetails);   // 商品详情
        mav.addObject("productNum",sum);   //  商品总数
        return mav;
    }



}
